home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_03 / stout / circbuf.hpp < prev    next >
C/C++ Source or Header  |  1995-02-06  |  5KB  |  141 lines

  1. //
  2. //  CIRCBUF.HPP - Header file for circular buffer C++ functions
  3. //
  4.  
  5. #ifndef _CIRCBUF_DEFINED_
  6. #define _CIRCBUF_DEFINED_
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. typedef enum {ASSIGNED = -1, ERROR = -1, SUCCESS, FALSE = 0, TRUE} boolean_t;
  12.  
  13. template<class T> class cbuf_t
  14. {
  15. private:
  16.       size_t      next;       // Wrap-around position pointer
  17.       boolean_t   full;       // TRUE when buffer has filled
  18.       boolean_t   ready;      // TRUE after init() call...
  19.                               //  ASSIGNED  after import() call...
  20.                               //   else FALSE
  21.  
  22. protected:
  23.       size_t      size;       // Number of elements
  24.       T         * buf;        // Pointer to circular buffer
  25.  
  26. public:
  27.       cbuf_t()           { buf = NULL; next = 0; full = ready = FALSE; };
  28.       cbuf_t(size_t len) { init(len); };
  29.       ~cbuf_t()          { if (TRUE == ready) delete buf; };
  30.  
  31.       void        init(size_t);
  32.       boolean_t   add(T);
  33.       boolean_t   data_ready() { return full; };
  34.       boolean_t   clear();
  35.       boolean_t   export(T **, size_t *);
  36.       void        import(T *, size_t);
  37. };
  38.  
  39. /************************************************************************/
  40. /*                                                                      */
  41. /*  init() Circular buffer intializer.                                  */
  42. /*                                                                      */
  43. /*  Arguments: 1 - Number of elements in the circular buffer.           */
  44. /*                                                                      */
  45. /************************************************************************/
  46.  
  47. template<class T> void cbuf_t<T>::init(size_t len)
  48. {
  49.       if (TRUE == ready)
  50.             delete buf;
  51.       else  ready = TRUE;
  52.       size = len;
  53.       buf  = new T[size];
  54.       clear();
  55. }
  56.  
  57. /************************************************************************/
  58. /*                                                                      */
  59. /*  clear() - Clear a circular buffer.                                  */
  60. /*                                                                      */
  61. /*  Returns: ERROR if buffer has not been initialized, else SUCCESS.    */
  62. /*                                                                      */
  63. /************************************************************************/
  64.  
  65. template<class T> boolean_t cbuf_t<T>::clear()
  66. {
  67.       if (!ready)
  68.             return ERROR;
  69.       memset(buf, 0, size * sizeof(T));
  70.       next = 0;
  71.       full = FALSE;
  72.       return SUCCESS;
  73. }
  74.  
  75. /************************************************************************/
  76. /*                                                                      */
  77. /*  add() - Function to add data to a circular buffer.                  */
  78. /*                                                                      */
  79. /*  Arguments: 1 - Data to add.                                         */
  80. /*                                                                      */
  81. /*  Returns: TRUE if buffer has been filled,                            */
  82. /*           FALSE if buffer has not filled,                            */
  83. /*           ERROR if buffer has not been initialized.                  */
  84. /*                                                                      */
  85. /************************************************************************/
  86.  
  87. template<class T> boolean_t cbuf_t<T>::add(T data)
  88. {
  89.       if (!ready)
  90.             return ERROR;
  91.       buf[next] = data;
  92.       if (size <= ++next)
  93.       {
  94.             next = 0;
  95.             full = TRUE;
  96.       }
  97.       return full;
  98. }
  99.  
  100. /************************************************************************/
  101. /*                                                                      */
  102. /*  export() - Publish a circular buffers location and length.          */
  103. /*                                                                      */
  104. /*  Arguments: 1 - Storage for buffer pointer.                          */
  105. /*             2 - Storage for size of buffer.                          */
  106. /*                                                                      */
  107. /*  Returns: ERROR if buffer has not been initialized, else SUCCESS.    */
  108. /*                                                                      */
  109. /************************************************************************/
  110.  
  111. template<class T> boolean_t cbuf_t<T>::export(T **buffer, size_t *len)
  112. {
  113.       if (!ready)
  114.             return ERROR;
  115.       *buffer = buf;
  116.       *len    = size;
  117.       return SUCCESS;
  118. }
  119.  
  120. /************************************************************************/
  121. /*                                                                      */
  122. /*  import() - Assign an existing circular buffer to a cbuf_t object.   */
  123. /*                                                                      */
  124. /*  Arguments: 1 - Buffer to assign.                                    */
  125. /*             2 - Size of buffer.                                      */
  126. /*                                                                      */
  127. /************************************************************************/
  128.  
  129. template<class T> void cbuf_t<T>::import(T *buffer, size_t len)
  130. {
  131.       if (TRUE == ready)
  132.             delete buf;
  133.       buf   = buffer;
  134.       size  = len;
  135.       ready = ASSIGNED;
  136. }
  137.  
  138. #endif // _CIRCBUF_DEFINED_
  139.  
  140.  
  141.